#pragma once
#include <string>
#include <memory>
#include <vector>
#include <iostream>
#include <fstream>
#include <mysql/mysql.h>
#include <jsoncpp/json/json.h>
#include <boost/algorithm/string.hpp>
#include <websocketpp/config/asio_no_tls.hpp>
#include <websocketpp/server.hpp>
using wsserver_t=websocketpp::server<websocketpp::config::asio>;

#include "logger.hpp"
class mysql_util
{
public:
    static MYSQL *mysql_create(const std::string &host,\
                               const std::string &username,\
                               const std::string &passwd,\
                               const std::string &dbname,\
                               int16_t port = 3306)
    {
        // 初始化句柄
        MYSQL *my = mysql_init(nullptr);
        if (my == nullptr)
        {
            // 句柄获取失败
            ELOG("mysql init failed!!!");
            return nullptr;
        }
        // 连接数据库
        if (nullptr == mysql_real_connect(my, host.c_str(), username.c_str(), passwd.c_str(), dbname.c_str(), port, NULL, 0))
        {
            // 数据库连接失败
            ELOG("mysql connect failed!!!,%s", mysql_error(my));
            mysql_close(my);
            return nullptr;
        }
        // 设置数据库字符集
        if (mysql_set_character_set(my, "utf8") != 0)
        {
            ELOG("mysql set character failed!!!");
            mysql_close(my);
            return nullptr;
        }
        return my;
    }
    static bool mysql_exec(MYSQL *mysql, const std::string &sql)
    {
        int n = mysql_query(mysql, sql.c_str());
        if (n != 0)
        {
            ELOG("sql:%s\n", sql.c_str());
            ELOG("mysql query failed!!!,:%s",mysql_error(mysql));
            return false;
        }
        return true;
    }
    static void mysql_destory(MYSQL *mysql)
    {
        if (mysql != nullptr)
        {
            mysql_close(mysql);
        }
        return;
    }
};

class json_util
{
public:
    static bool serialize(const Json::Value &in_vlaue, std::string *out_string)
    {
        Json::StreamWriterBuilder swb;
        std::unique_ptr<Json::StreamWriter> sw(swb.newStreamWriter());
        std::stringstream ss;
        int ret = sw->write(in_vlaue, &ss);
        if (ret != 0)
        {
            ELOG("serialize failed !!!");
            return false;
        }
        *out_string = ss.str();
        ;
        return true;
    }
    static bool unserialize(const std::string &in_string, Json::Value *out_vlaue)
    {
        Json::CharReaderBuilder crb;
        std::unique_ptr<Json::CharReader> cr(crb.newCharReader());
        Json::Value root;
        std::string err;
        bool ret = cr->parse(in_string.c_str(), in_string.c_str() + in_string.size(), &root, &err);
        if (ret == false)
        {
            ELOG("unserialize failed!!! :%s", err.c_str());
            return false;
        }
        *out_vlaue = root;
        return true;
    }
};

class string_util
{
public:
    static int strsplit(std::vector<std::string> &tokens, const std::string &str, const std::string &sep, bool is_compress = true)
    {
        boost::split(tokens, str, boost::is_any_of(sep), is_compress ? boost::token_compress_on : boost::token_compress_off);
        return tokens.size();
    }
};
class file_util
{
public:
    static bool readfile(const std::string &filename, std::string *out)
    {
        if(out==nullptr)
        {
            ELOG("out string is nullptr");
            return false;
        }
        std::ifstream ifs(filename, std::ios::binary);
        if (!ifs.is_open())
        {
            ELOG("%s file open error!", filename.c_str());
            return false;
        }
        size_t fsize = 0;
        ifs.seekg(0, std::ios::end); // 读取位置到文件末尾
        fsize = ifs.tellg();         // 获取文件读取位置的偏移量
        ifs.seekg(0,std::ios::beg);
        out->resize(fsize);
        ifs.read(&(*out)[0], fsize);
        DLOG("%ld",fsize);
        if (!ifs.good()) // 判断读取操作是否成功
        {
            ELOG("read %s error!", filename.c_str());
            ifs.close();
            return false;
        }
        ifs.close();
        return true;
    }
};